<template>
  <section>
    <el-dialog
      :visible.syng="dialogFormVisible"
      :title="title"
      @close="cleanForm"
    >
      <el-form :model="customerForm" :rules="customerRules" ref="UserDate">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" label-width="120px" prop="name">
              <el-input v-model="customerForm.name" :disabled="nameDisable"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" label-width="120px" prop="phone">
              <el-input v-model="customerForm.phone"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车牌号" label-width="120px" prop="carNum">
              <el-input v-model="customerForm.carNum"/>
            </el-form-item>
          </el-col>
          <el-col>
            <el-row>
              <el-form-item label="回访地址" label-width="120px" prop="address">
                <el-input v-model="customerForm.address"/>
              </el-form-item>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cleanForm">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import {validateCarNum, validatePhone} from "../../../../../utils/validate";

export default {
  name: "Add_Update_Dialog",
  data() {
    return {
      dialogFormVisible: false,
      title: '',
      customerForm: {
        name: '', phone: '', carNum: '', address: ''
      },
      nameDisable: false,
      customerRules: {
        name: [
          {required: true, message: '姓名不能为空', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {max: 11, message: '手机号最多11位'},
          {validator: validatePhone, trigger: 'blur'}
        ],
        carNum: [
          {required: true, validator: validateCarNum, trigger: 'blur'}
        ]
      }

    }
  },
  methods: {
    add() {
      this.dialogFormVisible = true
      this.title = '添加客户'
    },
    edit(customer) {
      this.dialogFormVisible = true
      this.title = '修改客户信息'
      this.nameDisable = true
      this.customerForm = customer
    },
    submit() { // 此方法不会修改用户表（t_user）中的信息，登录信息需要自己修改
      this.$axios.post('/customer/addWithUpdate', this.customerForm).then(resp => {
        if (resp.data.code === 200) {
          this.$message.success(resp.data.result)
          this.$emit("reload")
          this.dialogFormVisible = false
        }
      })
    },
    cleanForm() {
      this.dialogFormVisible = false
      this.customerForm = {name: '', phone: '', carNum: '', address: ''}
      this.nameDisable = false
    }
  }
}
</script>

<style scoped>

</style>
